package mysql

import (
	"gorm.io/gorm"
	"logincheck/logincheckrpc/logincheckrpc"
)

type Goods struct {
	gorm.Model
	GoodsName  string  `gorm:"type:varchar(30)"`
	GoodsPrice float32 `gorm:"type:decimal(10,2)"`
	UserId     int64   `gorm:"type:int(11)"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}
func GoodsList(db *gorm.DB, id int64) ([]*logincheckrpc.GoodsList, error) {
	//SELECT
	//  users.id,
	//  users.account,
	//  goods.goods_name,
	//  goods.goods_price
	//FROM
	//  goods
	//  LEFT JOIN users ON goods.user_id = users.id
	var list []*logincheckrpc.GoodsList
	err2 := db.Debug().Model(&Goods{}).Select(
		"users.id as user_id",
		"users.account",
		"goods.goods_name as goods_name",
		"goods.goods_price as goods_price",
	).
		Joins(" LEFT JOIN users ON goods.user_id = users.id").
		Where("users.id=?", id).
		Find(&list).Error
	return list, err2
}
